#include "SystemSDL_1_2.h"
#include "WindowSDL_1_2.h"
#include "../Main.h"

#include <SDL.h>
#include <cassert>

class CMainWindowSDL_1_2 : public CWindowSDL_1_2
{
    public:
        CMainWindowSDL_1_2( void );
        CMainWindowSDL_1_2( const CMainWindowSDL_1_2& wnd );
        CMainWindowSDL_1_2 operator=( const CMainWindowSDL_1_2& wnd );
virtual ~CMainWindowSDL_1_2( void );

        int Init( void );
        int Init( const char* title, const int width, const int height );
        int Process( void );
        void Refresh( void );
    private:
};

CMainWindowSDL_1_2::CMainWindowSDL_1_2( void )
{
    //ctor
}

CMainWindowSDL_1_2::~CMainWindowSDL_1_2( void )
{
    //dtor
}

int CMainWindowSDL_1_2::Init( void )
{
    m_pWnd = SDL_SetVideoMode( DEFAULT_WIDTH, DEFAULT_HEIGHT, 32, SDL_SWSURFACE );

    if( m_pWnd == NULL )
    {
        // an error occurred handle with care
        return -1;
    }

    SDL_WM_SetCaption( DEFAULT_TITLE, NULL );
    SDL_WM_GetCaption(  &m_pTitle, NULL );

    return 0;
}

int CMainWindowSDL_1_2::Init( const char* title, const int width, const int height )
{
    m_pWnd = SDL_SetVideoMode( width, height, 32, SDL_SWSURFACE );

    if( m_pWnd == NULL )
    {
        // an error has occurred, hanlde with care
        return -1;
    }

    assert( title );

    SDL_WM_SetCaption( title, NULL );
    SDL_WM_GetCaption( &m_pTitle, NULL );

    return 0;
}

int CMainWindowSDL_1_2::Process( void )
{
    return 0;
}

void CMainWindowSDL_1_2::Refresh( void )
{
    SDL_Flip( m_pWnd );
}



///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
/////////////                                                         /////////////////
/////////////                                                         /////////////////
/////////////         CSystemSDL_1_2 implementation start             /////////////////
/////////////                                                         /////////////////
/////////////                                                         /////////////////
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
CMainWindowSDL_1_2* CSystemSDL_1_2::m_pMainWnd = NULL;

int CSystemSDL_1_2::Run( CMain& engine )
{
    Init();

    while( CSystemSDL_1_2_Video::m_isRunning == true )
    {
        while( CSystemSDL_1_2_Video::Process() );
        engine.Run();
        m_pMainWnd->Refresh();
    }

    ShutDown();

    return 0;
}

int CSystemSDL_1_2::Init( void )
{
    // all pre-SDL initialization stuff goes here

    // start initialization of sub systems
    CSystemSDL_1_2_Helper::Init();
    CSystemSDL_1_2_Video::Init();

    //  all subsystems have been initialized now do main window
    m_pMainWnd = new CMainWindowSDL_1_2;

    if( m_pMainWnd->Init( "Help Me", 640, 480 ) < 0 )
    {
        // print out an error here and exit app
        return -1;
    }

    CSystemSDL_1_2_Renderer::Init();

    return 0;
}

void CSystemSDL_1_2::ShutDown( void )
{
    if( m_pMainWnd != NULL )
    {
        delete m_pMainWnd;
        m_pMainWnd = NULL;
    }
    // start shutting down the sub systems
    CSystemSDL_1_2_Renderer::ShutDown();
    CSystemSDL_1_2_Video::ShutDown();
    CSystemSDL_1_2_Helper::ShutDown();

    // ensure that SDL_Quit is the last function that is called
    SDL_Quit();
}

void CSystemSDL_1_2::Sleep( unsigned int ms )
{
    return SDL_Delay( ms );
}
